<?php
/**
 * Created by jinqiang on 2024/6/7 15:54.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Services\Banshi;


use App\Http\Models\Mysql\SsyNewProductBaseCodeTable;
use App\Http\Models\Mysql\SsyNewProductProgrammeTable;

class PlanService
{
    public function search($param)
    {
        $conn = (new SsyNewProductProgrammeTable())->where('status', (Int)$param['status']);
        if($param['base_category_id'] ?? ""){//基码id
            $conn = $conn->where('base_category_id', $param['base_category_id']);
        }
        if($param['programme_title'] ?? ""){
            $conn = $conn->where('programme_title', 'like', "%".$param['programme_title']."%");
        }
        if($param['base_category'] ?? ""){
            $conn = $conn->where('base_category', 'like', "%".$param['base_category']."%");
        }
        $res = $conn->orderBy('createtime', 'desc')->paginate((Int)$param['limit'])->toArray();
        foreach ($res['data'] as &$item) {
            $positionData = unserialize($item['position_data']);
            foreach($positionData as &$position){
                $position['id'] = (Int)$position['id'];
            }
            unset($position);
            $item['position_data'] = $positionData;
        }
        unset($item);
        return [
            'data' => $res['data'],
            'total' => $res['total']
        ];
    }

    public function setStatus($id, $status)
    {
        $data = ['status' => (Int)$status];
        return (new SsyNewProductProgrammeTable())->updateData($id, $data);
    }

    public function save($id, $param)
    {
        if(empty($param['base_category_id']) || empty($param['base_category']) || empty($param['programme_title'])
            || empty($param['position_data'])){
            return ['code' => -1, 'msg' => '保存内容不能为空'];
        }
        $data = [
            "programme_title" => $param['programme_title'],
            "position_data" => serialize($param['position_data']),
            "base_category_id" => $param['base_category_id'],
            "base_category" => $param['base_category'],
            "createtime"=>date("Y-m-d H:i:s")
        ];
        if(empty($id)){
            $exist = (new SsyNewProductProgrammeTable())->where('programme_title', $param['programme_title'])->exists();
            if($exist){
                return ['code' => -1, 'msg' => '方案已存在'];
            }
            return (new SsyNewProductProgrammeTable())->createData($data);
        }else{
            $exist = (new SsyNewProductProgrammeTable())->where('id', '<>', $id)->where('programme_title', $param['programme_title'])->exists();
            if($exist){
                return ['code' => -1, 'msg' => '方案已存在'];
            }
            unset($data['createtime']);
            return (new SsyNewProductProgrammeTable())->updateData($id, $data);
        }
    }

    //根据选中的服装类别和方案 出基码
    public function selectPlan($baseCodeId, $planId)
    {
        $baseCodeRes = (new SsyNewProductBaseCodeTable())->where('id', $baseCodeId)->first();
    }
}
